<?php

/**
 * feed actions.
 *
 * @package    begurholidays
 * @subpackage feed
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class feedActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }
  
  public function executeFeedag($request)
  {
  	$urlDomain = "http://www.begurholidays.com";
  	$urlFotos = "http://farm.costabravas.fr/";
  	//echo $request->getParameter('id'); 
  	$details = '<?xml version="1.0" encoding="utf-8" ?><feed>';
  	$details .= '<generatedAt>'.date("Y-m-d G:i:s").'</generatedAt>';
  	if($request->getParameter('id')!=0){
  		$homes = Doctrine_Query::create()->from('ViviendasAlquileres v')->where('v.usuario_id = ?', $request->getParameter('id'))->andWhere('v.activo = ?', '1')->execute();
  		//$details .= '<agency><name>fasááçñoñadfa</name><address>fasdfa</address></agency>';
  	}
  	else {
  		$homes = Doctrine_Query::create()->from('ViviendasAlquileres v')->andWhere('v.activo = ?', '1')->execute();
  	}
  	$details .= '<properties>';
  	
  	foreach ($homes as $home){
		  	$details .= '
		<property id="'.$home->getId().'">
		<name><![CDATA['. $home->getNombre().']]></name>
		<type>'. $home->getApartCasaName().'</type>	
		<sleeps>'. $home->getCapacidad().'</sleeps>			
		<city><![CDATA['.$home->getZonas()->getLocalidades()->getLocalidad().']]></city>';
		if($home->getZonas()){
		  	$details .= '<neighborhood><![CDATA['.$home->getZonas()->getZona().']]></neighborhood>';
		}
		if($home->getGeolatitud() && $home->getGeolongitud()){
		  	$details .= '<latitude>'.$home->getGeolatitud().'</latitude><longitude>'.$home->getGeolongitud().'</longitude>';
		}	  		  		
		$details .= '<environment>';
		if($home->getAmbito() == '0'){
			$details .= 'beach';
		}
		else{
			$details .= 'rural';
		}
		$details .= '</environment>';  		
		
		$details .= '<swimmingpool>'.$this->getPrivateComm($home->getPiscina()).'</swimmingpool>';
		$details .= '<floors>'.$home->getNumplantas().'</floors>';
		if($home->getBanos()){
			$details .= '<bathrooms>'.$home->getBanos().'</bathrooms>';
		}
		$details .= '<bedrooms num="'.$home->getHabitaciones().'">';
		if($home->getHabitacionesIndividual()){
			$details .= '<singlerooms>'.$home->getHabitacionesIndividual().'</singlerooms>';
		}
		if($home->getHabitacionesDoble()){
			$details .= '<doublerooms>'.$home->getHabitacionesDoble().'</doublerooms>';
		}
		if($home->getHabitacionesTriple()){
			$details .= '<triplerooms>'.$home->getHabitacionesTriple().'</triplerooms>';
		}
		if($home->getHabitacionesOtras()){
			$details .= '<otherrooms>'.$home->getHabitacionesOtras().'</otherrooms>';
		}
		$details .= '</bedrooms>';
		if($home->getCamasSupletorias()){
			$details .= '<extrabeds>'.$home->getCamasSupletorias().'</extrabeds>';
		}
		if($home->getDescCa()!="" || $home->getDescEs()!="" || $home->getDescEn()!="" || $home->getDescFr()!="" || $home->getDescDe()!="" || $home->getDescNl()!="" || $home->getDescIt()!=""){
		$details .= '<descriptions>';
		}
		if($home->getDescCaRed()!=""){
			$details .= '<description type="brief" lang="ca"><![CDATA['.strip_tags($home->getDescCaRed()).']]></description>';
		}
		if($home->getDescCa()!=""){
			$details .= '<description type="long" lang="ca"><![CDATA['.strip_tags(($home->getDescCa())).']]></description>';
		}
		if($home->getDescEsRed()!=""){
			$details .= '<description type="brief" lang="es"><![CDATA['.strip_tags($home->getDescEsRed()).']]></description>';
		}
		if($home->getDescEs()!=""){
			$details .= '<description type="long" lang="es"><![CDATA['.strip_tags($home->getDescEs()).']]></description>';
		}
		if($home->getDescEnRed()!=""){
			$details .= '<description type="brief" lang="en"><![CDATA['.strip_tags($home->getDescEnRed()).']]></description>';
		}
		if($home->getDescEn()!=""){
			$details .= '<description type="long" lang="en"><![CDATA['.strip_tags($home->getDescEn()).']]></description>';
		}
		if($home->getDescFrRed()!=""){
			$details .= '<description type="brief" lang="fr"><![CDATA['.strip_tags($home->getDescFrRed()).']]></description>';
		}
		if($home->getDescFr()!=""){
			$details .= '<description type="long" lang="fr"><![CDATA['.strip_tags($home->getDescFr()).']]></description>';
		}
		if($home->getDescDeRed()!=""){
			$details .= '<description type="brief" lang="de"><![CDATA['.strip_tags($home->getDescDeRed()).']]></description>';
		}
		if($home->getDescDe()!=""){
			$details .= '<description type="long" lang="de"><![CDATA['.strip_tags($home->getDescDe()).']]></description>';
		}
		if($home->getDescNlRed()!=""){
			$details .= '<description type="brief" lang="nl"><![CDATA['.strip_tags($home->getDescNlRed()).']]></description>';
		}
		if($home->getDescNl()!=""){
			$details .= '<description type="long" lang="nl"><![CDATA['.strip_tags($home->getDescNl()).']]></description>';
		}
		if($home->getDescItRed()!=""){
			$details .= '<description type="brief" lang="it"><![CDATA['.strip_tags($home->getDescItRed()).']]></description>';
		}
		if($home->getDescIt()!=""){
			$details .= '<description type="long" lang="it"><![CDATA['.strip_tags($home->getDescIt()).']]></description>';
		}
  		if($home->getDescCa()!="" || $home->getDescEs()!="" || $home->getDescEn()!="" || $home->getDescFr()!="" || $home->getDescDe()!="" || $home->getDescNl()!="" || $home->getDescIt()!=""){
			$details .= '</descriptions>';
		}
		if($home->getTv()>0){
			$details .= '<tv>true</tv>';
		}
		if($home->getWifi()>0){
			$details .= '<wifi>true</wifi>';
		}
		if($home->getSatelite()>0){
			$details .= '<tvsatellite>true</tvsatellite>';
		}
		if($home->getCalefaccion()>0){
			$details .= '<heating>true</heating>';
		}
		if($home->getAire()>0){
			$details .= '<airconditioning>true</airconditioning>';
		}
		if($home->getLavadora()>0){
			$details .= '<washingmachine>true</washingmachine>';
		}
		if($home->getLavavajillas()>0){
			$details .= '<dishwasher>true</dishwasher>';
		}
		if($home->getMicroondas()>0){
			$details .= '<microwaves>true</microwaves>';
		}
		if($home->getCafetera()>0){
			$details .= '<coffemachine>true</coffemachine>';
		}
		if($home->getTostadora()>0){
			$details .= '<toaster>true</toaster>';
		}
		if($home->getPlancha()>0){
			$details .= '<iron>true</iron>';
		}
		if($home->getDvd()>0){
			$details .= '<dvd>true</dvd>';
		}
		if($home->getChimenea()>0){
			$details .= '<chimney>true</chimney>';
		}
		$details .= '<garden>'.$this->getPrivateComm($home->getJardin()).'</garden>';
		$details .= '<garage>'.$this->getPrivateComm($home->getGaraje()).'</garage>';
		$details .= '<terrace>'.$this->getPrivateComm($home->getTerraza()).'</terrace>';
		
		if($home->getMueblesjardin()>0){
			$details .= '<gardenfurniture>true</gardenfurniture>';
		}	
		if($home->getBarbacoa()>0){
			$details .= '<barbacoa>true</barbacoa>';
		}
		if($home->getVistas()>0){
			$details .= '<views>true</views>';
		}
		if($home->getAnimales()>0){
			$details .= '<pets>true</pets>';
		}
		if($home->getFianza()>0){
			$details .= '<deposit>'.$home->getFianza().'</deposit>';
		}
		if($home->getPreciolimpieza()>0){
			$details .= '<deposit>'.$home->getPreciolimpieza().'</deposit>';
		}
		if($home->getAccesominusvalidos()>0){
			$details .= '<suitdisabled>true</suitdisabled>';
		}
		if($home->getDistplaya()){
			$details .= '<distbeach unit="kms">'.$home->getDistplaya().'</distbeach>';
		}
		
		if($home->getDiacambio()){
			$details .= '<changeday>'.$home->getDiaSemanaCambio().'</changeday>';
		}
		
		if($home->getPagoonline()==1){
			$details .= '<payonline>true</payonline>';
		}
		
		
		/*
		if(count($home->getAlquileresPrecios())>0){
			$details .= '<prices>'.count($home->getAlquileresPrecios()).'</prices>';
		}
		
		if($home->getPagoonline()!=1){
			//Tomamos como base _tablaPrecioNoPagoonline
			foreach ($home->getAlquileresPrecios() as $price){
				$checkDate = $price->getFechaInicio();
				//while ($checkDate < $price->getFechaFin()){
					$checkDateFin = date ("Y-m-d", strtotime ("+7 day", strtotime($checkDate)));
					//if($home->isDisponibleStateDay($home, $checkDate) && $price->isAlquilerPrecioVigenteArg($checkDateFin)){
						$details .= '<price currency="euro"><init-date>'.$checkDate.'</init-date>';
						
						$details .= '</price>';
					//}
				//}
			}
			$details .= '<payonline>true</payonline>';
		}*/
		
		$routing = $this->getContext()->getRouting();
		$details .= '<urls>';
		$details .= '<url lang="es">'.$urlDomain.$routing->generate('location_show_es', array ('slug' => $home->getSlug(), 'sf_culture' => 'es')).'</url>';
		$details .= '<url lang="ca">'.$urlDomain.$routing->generate('location_show_ca', array ('slug' => $home->getSlug(), 'sf_culture' => 'ca')).'</url>';
		$details .= '<url lang="en">'.$urlDomain.$routing->generate('location_show_en', array ('slug' => $home->getSlug(), 'sf_culture' => 'en')).'</url>';
		$details .= '<url lang="fr">'.$urlDomain.$routing->generate('location_show_fr', array ('slug' => $home->getSlug(), 'sf_culture' => 'fr')).'</url>';
		$details .= '<url lang="de">'.$urlDomain.$routing->generate('location_show_de', array ('slug' => $home->getSlug(), 'sf_culture' => 'de')).'</url>';
		$details .= '<url lang="nl">'.$urlDomain.$routing->generate('location_show_nl', array ('slug' => $home->getSlug(), 'sf_culture' => 'nl')).'</url>';
		//$details .= '<url lang="it">'.$urlDomain.$routing->generate('location_show_it', array ('slug' => $home->getSlug(), 'sf_culture' => 'it')).'</url>';
		$details .= '</urls>';
		if(count($home->getFoto())){
			$details .= '<media><fotos count="'.count($home->getFoto()).'">';
			foreach ($home->getFoto() as $foto){
				$details .= '<foto order="'.$foto->getOrderfoto().'" src="'.$urlFotos.$home->getUsuarioId().'/'.$home->getId().'/'.$foto->getNombrefoto().'" />';
			}
		    $details .= '</fotos></media>';
		}
		$details .= '<createdAt>'.$home->getCreatedAt().'</createdAt>';
		$details .= '<updatedAt>'.$home->getUpdatedAt().'</updatedAt>';
		$details .= '</property>';
  	}
  	$details .= '</properties></feed>';
  	$this->feed = $details;
  }
  
  private function getPrivateComm($aux){
  	switch ($aux) {
  		case 0:
  			return "false";
  			break;
  		case 1:
  			return "community";
  			break;
  		case 2:
  			return "private";
  			break;
  	}
  }
  
  private function getTrueFalse($aux){
  	switch ($aux) {
  		case 0:
  			return "false";
  			break;
  		case 1:
  			return "true";
  			break;
  	}
  }
 
}
